import copy
import json



def gen_true_collections(res):
    valid=[]
    for one in res:
        values = one['lab_result']
        for a in values:
            b=copy.copy(a)
            b['text_id'] = one['text_id']
            b['text'] = one['text']
            valid.append(b)
    return valid

def gen_pred_collections(res):
    valid=[]
    for one in res['submit_result']:
        values=one['mention_result']
        for a in values:
            b = copy.copy(a)
            b['text_id'] = one['text_id']
            b['text'] = one['text']
            b.pop('confidence')
            valid.append(b)
    return valid
def eval():
    valid_true_json = json.load(open('data/dev.json'))
    valid_pred_json = json.load(open('result/valid_result1.json'))
    a = set([json.dumps(tmp) for tmp in valid_pred_json['submit_result']])
    if len(a)==len(valid_pred_json['submit_result']):
        print('a')
    # valid_pred_json = json.load(open('result/valid_pred.json'))
    valid_true=gen_true_collections(valid_true_json)
    valid_pred=gen_pred_collections(valid_pred_json)
    b = set([json.dumps(tmp) for tmp in valid_pred])
    valid_pred=[json.loads(s) for s in b]
    if len(b) == len(valid_pred):
        print("b")
    common=0
    for i in valid_true:
        for j in valid_pred:
            if i==j:
                common+=1
    print(common)
    valid_true_len=len(valid_true)
    valid_pred_len=len(valid_pred)
    print(valid_true_len,valid_pred_len)
    precision=common/valid_pred_len
    recall=common/valid_true_len
    F1=2*precision*recall/(precision+recall)
    print(precision,recall,F1)

eval()